//
//  DSRegisgerViewModel.m
//  DSJY
//
//  Created by 冯华强 on 16/10/8.
//  Copyright © 2016年 dsjy. All rights reserved.
//  注册VM

#import "DSRegisgerViewModel.h"
#import "DSAccount.h"
#import "NSString+Extension.h"

@interface DSRegisgerViewModel ()
//初始化请求命令
@property (nonatomic, strong, readwrite) RACCommand *regisgerCommand;
@end

@implementation DSRegisgerViewModel

#pragma mark - 初始化
- (instancetype)init
{
    if (self = [super init]) {
        self.account = [[DSAccount alloc] init];
        //初始化
        [self initialization];
    }
    return self;
}

- (void)initialization
{
    //处理登录时的信号
    NSArray *combines = @[RACObserve(self.account, loginid), RACObserve(self.account, loginPassword), RACObserve(self.account, verifyPassword), RACObserve(self.account, mobile)];
    _regisgerEndSignal = [RACSignal combineLatest:combines reduce:^id(NSString *loginid, NSString *loginPassword, NSString *verifyPassword, NSString *mobile){
        
        return @(loginid.length && loginPassword.length && verifyPassword.length && mobile.length && self.checkBoxSelected);
    }];
    
    //加载请求
    [self createCmdSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        if (![[self.account.loginid trim] regexPattern:@"^$|^[^\\<\\>\%]{2,20}$"]) {
            [SVProgressHUD showErrorWithStatus:@"请输入2-20字!不能输入成对尖括号且里面带有英文,如：<英文>或/>"];
        }else if(![[self.account.loginPassword trim] regexPattern:@"[a-zA-Z0-9]{6,20}$"]){
            [SVProgressHUD showErrorWithStatus:@"你输入的密码格式[数字、英文6-20位]不正确!请重新输入!"];
        }else if (![[self.account.loginPassword trim] isEqualToString:[self.account.verifyPassword trim]]){
            [SVProgressHUD showErrorWithStatus:@"你输入的密码不一致!请重新输入!"];
        }else if ([self.account.mobile trim].length != 0 && ![[self.account.mobile trim] isMobileNumber]){
            [SVProgressHUD showErrorWithStatus:@"您输入正确的手机号码"];
        }else{
            [subscriber sendNext:nil];
        }
        [subscriber sendCompleted];
        return nil;
    }];
    
    //注册请求
    [self.requestCommand.executionSignals.switchToLatest subscribeNext:^(id x) {
        [_regisgerCommand execute:nil];
    }];
    
    // 处理注册验证业务逻辑
    _regisgerCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [DSNetworkTool regisgerWithParams:self.account success:^(NSDictionary *responseObject) {
                //返回值
                self.response = [YQResponse mj_objectWithKeyValues:responseObject];
                [self veriResponse:self.response finishedCallBack:^{
                    DSAccount *account = [DSAccount mj_objectWithKeyValues:[responseObject[@"attrs"] objectForKey:@"member"]];
                    MTLog(@"%@", account);
                    [subscriber sendNext:account];
                }];
                [subscriber sendCompleted];
            } failure:^(NSError *error) {
                [SVProgressHUD showErrorWithStatus:@"系统异常!"];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
    }];
    
    //设置提示
    [self executingWithCommand:_regisgerCommand];
}

@end
